<?php
/**
 *  购买道具
 */

class BuyProp extends APIBase {
	public $tag = "BuyProp";
	public $isLogin = true;
	
	public function before() {
		$this->initMysql();
		$this->inithttp();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	    
		if (!isset($this->param['propid']) || $this->param['propid'] == '') {
			$this->returnError(310, "propid must be needed");
		}
		
		//判断平台 1安卓  2苹果
		if (!isset($this->param['platform']) || $this->param['platform'] == '') {
			$this->returnError(310, "platform must be needed");
		}
		
		//判断是否独立包
		if (isset($this->param['independent']) && ($this->param['independent'] != 0 || $this->param['independent'] != '')){
			$independent = 1;
		}else{
			$independent = 0;
		}
	    $playerID = $this->uid;
		//判断是否为vip
		$is_vip = $this->data_redis->hGet("hu:".$playerID,"is_vip");
		
		$where['id'] = (int)$this->param['propid'];//商品id
		$goods = $this->mysql->select('goods','*',$where);
		if(!count($goods)) {     
			$this->returnError(310, "propid is not exists");
		}
		
		//$user = $this->data_redis->hMget("hu:{$this->uid}", array('round_card_num'));//玩家的的局数卡与IM信息

		//商品信息
		$name = $goods[0]['name'];
        $buy_money = (int)$goods[0]['buy_money'];
		$platform = (int)$this->param['platform'];
		
		if($platform == 1 || $platform == 2 ){
			$payment = $this->data_redis->hGet("hu:".$playerID,'payment');
			if ($payment != 3){
				$this->data_redis->hSet("hu:".$playerID,'payment',$payment);
			}else{
				$this->data_redis->hSet("hu:".$playerID,'payment',1);
			}
		}
		
		if($platform == 2 && $independent == 1){//苹果独立包需要改变默认支付方式
			$this->data_redis->hSet("hu:".$playerID,'payment',3);
		}
		
		
		 
		$payment = $this->data_redis->hGet("hu:".$playerID,'payment');
        $row = $this->cache_redis->hMGet('hp:'.$payment,array('id','name','pay_type'));
	    $data = array('paymentid'=>$row['id'],'name'=>$name,'price'=>$buy_money,'paymentname'=>$row['name'],'pay_type'=>$row['pay_type']);

	    $this->returnData($data);
		
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
